package dnl.confij.xml;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import dnl.confij.ConfigException;
import dnl.confij.Configuration;

public class XmlConfiguration extends Configuration {

	/**
	 * 
	 * Constructs a new <code>XmlConfiguration</code> using the given Xml
	 * file.
	 * 
	 * @param XmlFile
	 * @throws ConfigException
	 */
	public XmlConfiguration(File xmlFile) throws ConfigException {
		this(xmlFile, false, null);
	}

	/**
	 * 
	 * Constructs a new <code>XmlConfiguration</code> using the given Xml file
	 * and the given charset.
	 * 
	 * @param XmlFile
	 * @param charset
	 *            a character set that corresponds to the file's encoding.
	 * @throws ConfigException
	 */
	public XmlConfiguration(File xmlFile, String charset)
			throws ConfigException {
		this(xmlFile, false, charset);
	}

	/**
	 * 
	 * Constructs a new <code>PropertiesConfiguration</code> using the given
	 * properties file and the given charset.
	 * 
	 * @param xmlFile
	 * @param createFileIfNeeded
	 *            creates the properties file if one does not exist.
	 * @param charset
	 *            a character set that corresponds to the file's encoding.
	 * @throws ConfigException
	 */
	public XmlConfiguration(File xmlFile, boolean createFileIfNeeded,
			String charset) throws ConfigException {
		if (createFileIfNeeded && !xmlFile.exists()) {
			try {
				FileUtils.touch(xmlFile);
			} catch (IOException e) {
				throw new ConfigException("Could not create file.", e);
			}
		}
		if (charset == null) {
			this.configSource = new XmlConfigurationSource(xmlFile);
		} else {
			this.configSource = new XmlConfigurationSource(xmlFile, charset);
		}
		this.configSource.loadConfiguration(this);
	}
}
